package com.module.codeBind.dao;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Session;

import base.dao.GenericHibernateDao;

import com.module.codeBind.vo.BindBatchCode;

public class BindBatchCodeDao extends GenericHibernateDao<BindBatchCode,String>{

	private static BindBatchCodeDao dao = new BindBatchCodeDao();
	private BindBatchCodeDao(){}
	public static BindBatchCodeDao getInstance(){
		return dao;
	}
	public boolean save(Session session,List itemList,String bindId){
		this.deleteByQuery(session,"delete from BindBatchCode as obj where obj.bindId='"+bindId+"'");
		for(Iterator it = itemList.iterator();it.hasNext();){
			BindBatchCode bean = new BindBatchCode();
			bean.setBatchCodeId((String)it.next());
			bean.setBindId(bindId);
			session.save(bean);
		}
		return true;
	}
	public List getListByBindId(Session session,String bindId){
		String hql = "select batchCodeId from BindBatchCode as obj  where bindId ='"+bindId+"'";
		return session.createQuery(hql).list();
	}
	/**
	 * 批次是否已经绑定应用
	 * @param session
	 * @param list
	 * @return
	 */
	public boolean hasBatchCode4Bind(Session session,List list){
		for(Iterator it = list.iterator();it.hasNext();){
			String hql = "select bci from CodeBind as bc,BindBatchCode as bci where bc.id= bci.bindId and bc.status!='D' and bci.batchCodeId = '"+it.next()+"'";
			List<BindBatchCode> itemList = this.findBySQL(session, hql);
			if(itemList == null || itemList.isEmpty()){
				return false;
			}
		}
		
		return true;
		
	}
}
